package com.tanhua.server.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.tanhua.model.dto.RecommendQueryDto;
import com.tanhua.model.vo.NearUserVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.TodayBest;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.TanhuaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tanhua")
public class TanhuaController {

    @Autowired
    private TanhuaService tanhuaService;

    @GetMapping("/todayBest")
    public ResponseEntity todayBest(){
        TodayBest todayBest = tanhuaService.todayBest();
        return ResponseEntity.ok(todayBest);
    }

    @GetMapping("/recommendation")
    public ResponseEntity recommendList(RecommendQueryDto recommendQueryDto){
        PageResult pageResult = tanhuaService.recommendList(recommendQueryDto);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("/{id}/personalInfo")
    public ResponseEntity personalInfo(@PathVariable("id") Long recommendUserId){
        TodayBest todayBest = tanhuaService.personalInfo(recommendUserId);
        return ResponseEntity.ok(todayBest);
    }

    @GetMapping("/strangerQuestions")
    public ResponseEntity strangerQuestions(Long userId){
        String question = tanhuaService.question(userId);
        return ResponseEntity.ok(question);
    }

    @PostMapping("/strangerQuestions")
    public ResponseEntity replyQuestion(@RequestBody Map<String,String> map) throws JsonProcessingException {
        Long targetUserId = Long.valueOf(map.get("userId"));
        String reply = map.get("reply");
        tanhuaService.replyQuestion(targetUserId,reply);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/cards")
    public ResponseEntity cards(){
        List<TodayBest> todayBestList = tanhuaService.queryCardsList();
        return ResponseEntity.ok(todayBestList);
    }

    @GetMapping("/{id}/love")
    public ResponseEntity likeUser(@PathVariable("id") Long likeUserId) {
        this.tanhuaService.likeUser(likeUserId);
        return ResponseEntity.ok(null);
    }


    @GetMapping("{id}/unlove")
    public ResponseEntity notLikeUser(@PathVariable("id") Long likeUserId) {
        this.tanhuaService.notLikeUser(likeUserId);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/search")
    public ResponseEntity searchNearUser(String gender,Integer distance) {
        List<NearUserVo> voList = tanhuaService.searchNearUser(gender,distance);
        return ResponseEntity.ok(voList);
    }
}
